package ServletCooking;

/**
 * @author 李某
 * * @date 2021/11/20
 * *物有本末，事有终始。知其先后者，则近道矣！
 * *荷花开满池塘的前一天还是半塘荷花半塘云。
 */


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

// 测试的Cooking

/**
 * Servlet implementation class HelloServlet
 */
//@WebServlet("/HelloForm")
public class HelloForm extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloForm() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 为名字和姓氏创建 Cookie
        Cookie name = new Cookie("name",
                URLEncoder.encode(request.getParameter("name"), "UTF-8")); // 中文转码
        Cookie url = new Cookie("url",
                request.getParameter("url"));

        // 为两个 Cookie 设置过期日期为 24 小时后
        name.setMaxAge(60 * 60 * 24);
        url.setMaxAge(60 * 60 * 24);

        // 在响应头中添加两个 Cookie
        response.addCookie(name);
        response.addCookie(url);

        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        String title = "设置 Cookie 实例";
        String docType = "<!DOCTYPE html>\n";
        out.println(docType +
                "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n" +
                "<ul>\n" +
                "  <li><b>站点名：</b>："
                + request.getParameter("name") + "\n</li>" +
                "  <li><b>站点 URL：</b>："
                + request.getParameter("url") + "\n</li>" +
                "</ul>\n" +
                "</body></html>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}


/**
 * 配套的XML
 * <?xml version="1.0" encoding="UTF-8"?>
 * <web-app>
 *   <servlet>
 *     <!-- 类名 -->
 *     <servlet-name>HelloForm</servlet-name>
 *     <!-- 所在的包 -->
 *     <servlet-class>com.runoob.test.HelloForm</servlet-class>
 *   </servlet>
 *   <servlet-mapping>
 *     <servlet-name>HelloForm</servlet-name>
 *     <!-- 访问的网址 -->
 *     <url-pattern>/TomcatTest/HelloForm</url-pattern>
 *   </servlet-mapping>
 * </web-app>
 */

/**
 * 配套的HTML文件
 * <!DOCTYPE html>
 * <html>
 * <head>
 * <meta charset="utf-8">
 * <title>菜鸟教程(runoob.com)</title>
 * </head>
 * <body>
 * <form action="/TomcatTest/HelloForm" method="GET">
 * 站点名 ：<input type="text" name="name">
 * <br />
 * 站点 URL：<input type="text" name="url" /><br>
 * <input type="submit" value="提交" />
 * </form>
 * </body>
 * </html>
 */



